/*
 * Date Format 1.2.3
 * (c) 2007-2009 Steven Levithan <stevenlevithan.com>
 * MIT license
 *
 * Includes enhancements by Scott Trenda <scott.trenda.net>
 * and Kris Kowal <cixar.com/~kris.kowal/>
 *
 * Accepts a date, a mask, or a date and a mask.
 * Returns a formatted version of the given date.
 * The date defaults to the current date/time.
 * The mask defaults to dateFormat.masks.default.
 */

var dateFormat = function () {
	var	token = /d{1,4}|m{1,4}|yy(?:yy)?|([HhMsTt])\1?|[LloSZ]|"[^"]*"|'[^']*'/g,
		timezone = /\b(?:[PMCEA][SDP]T|(?:Pacific|Mountain|Central|Eastern|Atlantic) (?:Standard|Daylight|Prevailing) Time|(?:GMT|UTC)(?:[-+]\d{4})?)\b/g,
		timezoneClip = /[^-+\dA-Z]/g,
		pad = function (val, len) {
			val = String(val);
			len = len || 2;
			while (val.length < len) val = "0" + val;
			return val;
		};

	// Regexes and supporting functions are cached through closure
	return function (date, mask, utc) {
		var dF = dateFormat;

		// You can't provide utc if you skip other args (use the "UTC:" mask prefix)
		if (arguments.length == 1 && Object.prototype.toString.call(date) == "[object String]" && !/\d/.test(date)) {
			mask = date;
			date = undefined;
		}

		// Passing date through Date applies Date.parse, if necessary
		date = date ? new Date(date) : new Date;
		if (isNaN(date)) return "";//throw SyntaxError("invalid date");

		mask = String(dF.masks[mask] || mask || dF.masks["default"]);

		// Allow setting the utc argument via the mask
		if (mask.slice(0, 4) == "UTC:") {
			mask = mask.slice(4);
			utc = true;
		}

		var	_ = utc ? "getUTC" : "get",
			d = date[_ + "Date"](),
			D = date[_ + "Day"](),
			m = date[_ + "Month"](),
			y = date[_ + "FullYear"](),
			H = date[_ + "Hours"](),
			M = date[_ + "Minutes"](),
			s = date[_ + "Seconds"](),
			L = date[_ + "Milliseconds"](),
			o = utc ? 0 : date.getTimezoneOffset(),
			flags = {
				d:    d,
				dd:   pad(d),
				ddd:  dF.i18n.dayNames[D],
				dddd: dF.i18n.dayNames[D + 7],
				m:    m + 1,
				mm:   pad(m + 1),
				mmm:  dF.i18n.monthNames[m],
				mmmm: dF.i18n.monthNames[m + 12],
				yy:   String(y).slice(2),
				yyyy: y,
				h:    H % 12 || 12,
				hh:   pad(H % 12 || 12),
				H:    H,
				HH:   pad(H),
				M:    M,
				MM:   pad(M),
				s:    s,
				ss:   pad(s),
				l:    pad(L, 3),
				L:    pad(L > 99 ? Math.round(L / 10) : L),
				t:    H < 12 ? "a"  : "p",
				tt:   H < 12 ? "am" : "pm",
				T:    H < 12 ? "A"  : "P",
				TT:   H < 12 ? "AM" : "PM",
				Z:    utc ? "UTC" : (String(date).match(timezone) || [""]).pop().replace(timezoneClip, ""),
				o:    (o > 0 ? "-" : "+") + pad(Math.floor(Math.abs(o) / 60) * 100 + Math.abs(o) % 60, 4),
				S:    ["th", "st", "nd", "rd"][d % 10 > 3 ? 0 : (d % 100 - d % 10 != 10) * d % 10]
			};

		return mask.replace(token, function ($0) {
			return $0 in flags ? flags[$0] : $0.slice(1, $0.length - 1);
		});
	};
}();

// Some common format strings
dateFormat.masks = {
	"default":      "ddd mmm dd yyyy HH:MM:ss",
	shortDate:      "m/d/yy",
	mediumDate:     "mmm d, yyyy",
	longDate:       "mmmm d, yyyy",
	fullDate:       "dddd, mmmm d, yyyy",
	shortTime:      "h:MM TT",
	mediumTime:     "h:MM:ss TT",
	longTime:       "h:MM:ss TT Z",
	isoDate:        "yyyy-mm-dd",
	isoTime:        "HH:MM:ss",
	isoDateTime:    "yyyy-mm-dd'T'HH:MM:ss",
	isoUtcDateTime: "UTC:yyyy-mm-dd'T'HH:MM:ss'Z'"
};

// Internationalization strings
dateFormat.i18n = {
	dayNames: [
		"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat",
		"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
	],
	monthNames: [
		"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec",
		"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"
	]
};

// For convenience...
Date.prototype.format = function (mask, utc) {
	return dateFormat(this, mask, utc);
};


/***************************************************************************************************************/


var dateUtils={
	/**
	*timeticks : java Date.getTime()的值
	*formatExp: yyyy-mm-dd HH:MM:ss
	*返回格式化后的字符串 
	*/
	format : function(milliseconds, formatExp){
		if(milliseconds === null) return "";
		if (!milliseconds && typeof(milliseconds)=="undefined" && milliseconds!=0) return "";
		if(typeof(milliseconds)=="string"){
			if(milliseconds == ''){
				return "";
			}
			milliseconds = parseInt(milliseconds);
		}
		if(!formatExp) formatExp = 'yyyy-mm-dd';
		
		var dtime = new Date();
		dtime.setTime(milliseconds);
		var text = dtime.format(formatExp);
		return text;
	},
		/**
	 * 获取当前年份的前几年
	 * 
	 * @param num
	 * @returns {Date}
	 */
	getLastYear : function(num) {
		var myDate = new Date();
		myDate.setFullYear(myDate.getFullYear() - num);
		return myDate;
	},
	/**
	 * 获取当前年份的前几月
	 * 
	 * @param num
	 * @returns {Date}
	 */
	getLastMonth : function(num) {
	//	debugger;
		var mydate = new Date();
		var year = mydate.getFullYear(); // 获取当前日期的年份
		var month = mydate.getMonth() + 1; // 获取当前日期的月份
		var day = mydate.getDate(); // 获取当前日期的日
		var days = new Date(year, month, 0);
		days = days.getDate(); // 获取当前日期中月的天数
		var year2 = year;
		var month2 = parseInt(month) - num;
		if (month2 == 0) {
			year2 = parseInt(year2) - 1;
			month2 = 12;
		}else if(month2<0){
			year2 = parseInt(year2) - 1;
			month2=12+month2;
		}
		var day2 = day;
		var days2 = new Date(year2, month2, 0);
		days2 = days2.getDate();
		if (day2 > days2) {
			day2 = days2;
		}
		if (month2 < 10) {
			month2 = '0' + month2;
		}
		var lastMoth = year2 + '-' + month2 + '-' + day2;
		return new Date(lastMoth);
	},

	/**
	 * 获取当前日期的前几天
	 * 
	 * @param num
	 * @returns {Date}
	 */
	getLastDay : function(num) {
		var today = new Date();
		var yesterday_milliseconds = today.getTime() - 1000 * 60 * 60 * 24 * num;
		var yesterday = new Date();
		yesterday.setTime(yesterday_milliseconds);
		var strYear = yesterday.getFullYear();
		var strDay = yesterday.getDate();
		var strMonth = yesterday.getMonth() + 1;
		if (strMonth < 10) {
			strMonth = "0" + strMonth;
		}
		if(strDay<10){
			strDay="0"+strDay;
		}
		var strYesterday = strYear + "-" + strMonth + "-" + strDay;
		return new Date(strYesterday);
	},
	// 获取当前周
	theWeek : function() {
		var totalDays = 0;
		now = new Date();
		years = now.getYear();
		if (years < 1000)
			years += 1900;
		var days = new Array(12);
		days[0] = 31;
		days[2] = 31;
		days[3] = 30;
		days[4] = 31;
		days[5] = 30;
		days[6] = 31;
		days[7] = 31;
		days[8] = 30;
		days[9] = 31;
		days[10] = 30;
		days[11] = 31;
		// 判断是否为闰年，针对2月的天数进行计算
		if (Math.round(now.getYear() / 4) == now.getYear() / 4) {
			days[1] = 29;
		} else {
			days[1] = 28;
		}
		if (now.getMonth() == 0) {
			totalDays = totalDays + now.getDate();
		} else {
			var curMonth = now.getMonth();
			for ( var count = 1; count <= curMonth; count++) {
				totalDays = totalDays + days[count - 1];
			}
			totalDays = totalDays + now.getDate();
		}
		// 得到第几周
		var week = Math.round(totalDays / 7);
		return week;
	},
	  /**
	  * 获取date所在的周的起始日期和终止日期
	  * 比如date=2013-1-11 则返回2013-1-7和2013-1-13
	  * @param {} date
	  * @return {}
	  */
	  getWeekDays : function(date){
		  var now=date;
		  
		  var start=new Date();
		  var end=new Date();
		
		  var weekday=now.getDay(); //从 Date 对象返回一周中的某一天 (0 ~ 6)。 返回值是 0（周日） 到 6（周六） 之间的一个整数。
		  if(weekday == 0) weekday = 6;
		  else weekday = weekday - 1;
	
		  var nowTimes = now.getTime();//返回 1970 年 1 月 1 日至今的毫秒数。
		  var weekdayTimes = weekday * 24 * 3600 * 1000;
		  var week1Times = nowTimes - weekdayTimes;
		  var week7Times = week1Times + 6 * 24 * 3600 * 1000;
		  start.setTime(week1Times);
		  end.setTime(week7Times);
		  
		  var obj = {};
		  obj.startDate = start;
		  obj.endDate = end;
		  return obj;
	 },
	 
	 /**数字代表的时间戳 毫米数*/
	 getDateFromTimes:function(times){
		
		 var date = new Date();
		 if(times){
			 date.setTime(times);
		 }
	//	 var date = new Date(parseInt(times) * 1000).toLocaleString().replace(/:\d{1,2}$/,' ');   
		 return date;
	 },
	 
	 /**时间戳转时间,数字代表的时间戳 毫米数*/
	 getFormatDateTimeFromTimes:function(times){
		 if(times === null) return "";
		 if (!times && typeof(times)=="undefined" && times!=0) return "";
		 var date = this.getDateFromTimes(times);
		 var str = date.format("yyyy-mm-dd HH:MM:ss");
		 return str;
	 },
	 
	  /**时间戳转时间,数字代表的时间戳 毫米数*/
	 getFormatDateFromTimes:function(times){
		 if(times === null) return "";
		 if (!times && typeof(times)=="undefined" && times!=0) return "";
		 if(typeof(times) === 'string'){
			 if(times.indexOf('-') > 0  || times.indexOf('/') > 0  || times.indexOf('年') > 0){
			 var datestr = times; //yyyy-mm-dd hh:mm:ss
			 var spIdx = datestr.indexOf(' ');
			 if(spIdx > 0){
				 datestr = datestr.substring(0, spIdx);//截取日期部分，忽略时间部分
			 }
			 return datestr;
			 }
		 }

		 var date = this.getDateFromTimes(times);
		 var str = date.format("yyyy-mm-dd");
		 return str;
	 },
	 
	 /**
	  * 将时间格式转换为时间戳
	  * @param str  时间格式
	  * @return
	  */
	 dateToStamp:function(str){
		 if(str==null || str.length == 0) return null;
		 var new_str = str.replace(/:/g,'-');
		 new_str = new_str.replace('年','-');
		 new_str = new_str.replace('月','-');
		 new_str = new_str.replace('日','-');
		 new_str = new_str.replace('时','-');
		 new_str = new_str.replace('分','-');
		 new_str = new_str.replace('秒','-');
		 new_str = new_str.replace('+',' ');
		 new_str = new_str.replace('T',' ');
		 new_str = new_str.replace(/ /g,'-');
		 var arr = new_str.split("-");
		 var datum = null;
		 if(arr.length == 3){
			 datum=new Date(Date.UTC(arr[0],arr[1]-1,arr[2],0-8,0,0));
		 }else if(arr.length == 6){
			datum=new Date(Date.UTC(arr[0],arr[1]-1,arr[2],arr[3]-8,arr[4],arr[5]));
		 }
		// var datum = new Date(datum.getTime());
		// alert("<br><b>还原到原始日期为</b>: "+datum.toLocaleString());
		 return datum.getTime();
	}
	
};